﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dzs.Web.Admin.distribution
{
    public partial class MyLink : Web.Admin.BasePage
    {
        Maticsoft.BLL.monthSupplayPlan bll = new Maticsoft.BLL.monthSupplayPlan();        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["login"] != null)
                {
                    BLL.configinfo bc = new BLL.configinfo();
                    Model.configinfo c = bc.GetModel();
                    lblmsg.Text = "配送月开始日期为：" + c.psyqs.Value.ToString() + "，配送月结束日期为：" + c.psyjs.Value.ToString();
                    BindData();
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        #region gridView

        public void BindData()
        {
            int id = getid();
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" and user_id=" + id);            
            if (txtKeyword3.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and sname like '%{0}%'", txtKeyword3.Text.Trim());
            }
            strWhere.Append(" order by sname");
            ds = bll.GetListlink(strWhere.ToString());
            gridView.DataSource = ds;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        } 

        #endregion

        protected void btnlld_Click(object sender, EventArgs e)
        {
            int id = getid();
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" and user_id=" + id);           
            if (txtKeyword3.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and sname like '%{0}%'", txtKeyword3.Text.Trim());
            }
            strWhere.Append(" order by sname");
            ds = bll.GetListlink(strWhere.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                string file = ExWord.ExportTops(ds);
                FileInfo DownloadFile = new FileInfo(file);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();
            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "数据为空！");
            }

        }
    }
}